package com.example.demo.JsonTest;

import com.example.demo.JsonTest.Entity.FinancialMonthEntity;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
 * @author LHH
 * @date 2025/4/15
 */
public class JsonBTest {

    public static void main(String[] args) {
        String folderPath = "C:\\Users\\admin\\Desktop\\Work\\沿海\\物资库对接\\JsonList";
        try {
            List<Object> contents = readFilesInFolder(folderPath);
            for (int i = 0; i < contents.size(); i++) {
                System.out.println("文件 " + (i + 1) + " 的内容:\n" + contents.get(i));
            }
        } catch (IOException e) {
            System.err.println("发生 I/O 错误: " + e.getMessage());
        }
    }

    private static List<FinancialMonthEntity> readFileContent(Path filePath) throws IOException {
        List<FinancialMonthEntity> resultList = new ArrayList<>();
        ObjectMapper objectMapper = new ObjectMapper();

        // 如果版本支持，仍可使用此配置（2.5+）
        // objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);

        try (BufferedReader reader = Files.newBufferedReader(filePath)) {
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.trim().isEmpty()) continue;

                List<FinancialMonthEntity> lineList = objectMapper.readValue(
                        line,
                        new TypeReference<List<FinancialMonthEntity>>() {}
                );
                resultList.addAll(lineList);
            }
        }
        return resultList;
    }

    public static List<Object> readFilesInFolder(String folderPath) throws IOException {
        List<Object> fileContents = new ArrayList<>();
        Path folder = Paths.get(folderPath);
        if (Files.exists(folder) && Files.isDirectory(folder)) {
            try (Stream<Path> paths = Files.walk(folder)) {
                paths.filter(Files::isRegularFile).forEach(filePath -> {
                    try {
                        List<FinancialMonthEntity> content = readFileContent(filePath);
                        fileContents.add(content);
                    } catch (IOException e) {
                        System.err.println("读取文件 " + filePath + " 时出错: " + e.getMessage());
                    }
                });
            }
        } else {
            System.err.println("指定的路径不是有效的文件夹: " + folderPath);
        }
        return fileContents;
    }

}


